<?php

require_once 'web.php';

/**
 * Description of HomeController
 *
 * @author user
 */
class AuthpageController extends WebController {
    protected $view = 'auth_view';

    public function  _before() {
        parent::_before();
        if (!$this->auth_user->is_login()) {
            URL::redirect('home');
        }

        /**
         * GET USER
         */
        $user = $this->view_user;

        /**
         * AGE
         */
        $user_age =  $this->_calculate_age($user->birth_date);

        
        /**
         * GET PROFILE PICTRUE
         */
        $photo = new ORM('photo');
        $photo->find($user->photo_id);
        $user_profic = $photo->link;

        
        /*
         * FORM EDIT PROFILE
         */
        $form = View::factory('register/index');
        $form->action = URL::site('profile/edit/' . $user->id);
        $form->show_username = FALSE;
        $form->user = $user;
        $form->submit_value = "Edit!";
        
        $locations = new ORM('location');
        $form->locations = $locations->find_all();
        
        $access = new ORM('access');
        $form->access = $access->find_all();
        /*
         * FOLLOWINGS
         */
        $query = "
            SELECT *
            FROM `followings`
            JOIN `user` ON `user`.`id` = `followings`.`followee`
            LEFT JOIN `photo` ON `photo`.`id` = `user`.`photo_id`
            WHERE `followings`.`follower` = $user->id";
        $followings = DBQuery::instance()->query($query);

        /*
         * FOLLOWERS
         */
        $query = "
            SELECT *
            FROM `followings`
            JOIN `user` ON `user`.`id` = `followings`.`follower`
            LEFT JOIN `photo` ON `photo`.`id` = `user`.`photo_id`
            WHERE `followings`.`followee` = $user->id";
        $followers = DBQuery::instance()->query($query);

        /*
         * SET VIEW
         */
        $data = array();
        $data['form']   = $form;
        $data['user']   = $user;
        $data['auth_user']   = $this->auth_user->user;
        $data['user_profic'] = $user_profic;
        $data['user_age']    = $user_age;
        $data['view_user']   = $this->view_user;
        $data['other_user']  = $this->_is_other_user();
        $data['show_follow'] = $this->_show_follow();
        $data['followings']  = $followings;
        $data['followers']   = $followers;

        $this->view->set($data);
    }

    private function _calculate_age($birth_date) {
        return (int)((time() - strtotime($birth_date)) / 60 / 60 / 24 / 360);
    }

    protected function _is_other_user() {
        return $this->view_user->id != $this->auth_user->user->id;
    }

    protected function _show_follow() {
        $is_other_user = $this->_is_other_user();
        if($is_other_user) {
            $view_user = $this->view_user;
            $auth_user = $this->auth_user->user;
            $query = "
                SELECT *
                FROM `followings`
                WHERE `follower`=$auth_user->id
                AND `followee`=$view_user->id";
            $result = DBQuery::instance()->query($query);
            if(sizeof($result) == 0) {
                return TRUE;
            }
        }

        return FALSE;
    }

    public function _after() {

        $user = $this->view_user;
        $get = "";
        
        if ($user->id != $this->auth_user->user->id) {
            $get = "?uid=$user->id";

            //Links
            $profile_link = array(
                "photo$get"     => array('value' => 'Photo'),
                "wall$get"      => array('value' => 'Wall'),
            );
        }
        else {
            $profile_link = array(
                "newsfeed$get"  => array('value' => 'Notification', 'id' => 'profile-notif'),
                "photo$get"     => array('value' => 'Photo'),
                "wall$get"      => array('value' => 'Wall'),
                "message$get"   => array('value' => 'Message'),
            );
        }

        $controller = $this->router->controller;
        $profile_link[$controller . $get]['active'] = true;

        $this->view->profile_link = $profile_link;

        ///Chat thingies
        //Info about this user
        $this->view->chat_content = View::factory("chat/index");
        $chat_user = new ORM('chat_user');
        $chat_user->where("`user_id` = " . $this->auth_user->user->id)->find();
        if($chat_user->id == null) {
            $chat_user->user_id = $this->auth_user->user->id;
            $chat_user->last_conn = date("Y-m-d H:i:s", time());
            $chat_user->last_update = date("Y-m-d H:i:s", time());
            $chat_user->status = 1;
            $chat_user->status_message = "dummy";
            $chat_user->is_online = 1;
            $chat_user->save();
        }
        $chat_user->username = $this->auth_user->user->username;
        $this->view->chat_content->my_info = $chat_user->toJsonString();

        //Links to images
        $chatstatuses = new ORM('chat_status');
        $chatstatuses = $chatstatuses->find_all();
        $chatstatusesarr = array();
        foreach ($chatstatuses as $chatstatus) {
            $chatstatusesarr[$chatstatus->id] = $chatstatus->getData();
            $chatstatusesarr[$chatstatus->id]['icon_link'] = URL::base_url() . $chatstatusesarr[$chatstatus->id]['icon_link'];
        }
        $this->view->chat_content->chat_images = json_encode($chatstatusesarr);

        parent::_after();
    }
}

?>
